<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Coreorder as ModelCoreorder;

use wxpay\WxPayNotify;
/**
 * 公共接口
 * @ApiWeigh (998)
 * 
 */
class Coreorder extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['notify'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // protected $auth = ['test3'];

    /**
     * 支付订单
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="int", required=false, description="核心订单ID")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function pay()
    {
        $user = $this->auth->getUser();
        $rej = $this->validate(
            $this->request->post(),
            [
                'id' => 'require|number',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $order_info = ModelCoreorder::get($this->request->post('id'));

        //①、获取用户openid
        if (!$user->openid) {
            $this->responseMsg(2006, '', '用户状态异常！');
        }
        if ($order_info->module_mark == 'shop') {
            // $shoporder = Order::get($order_info->order_id);
            // if (!$shoporder) {
            //     $this->responseMsg(2006, '', '没有找到订单！');
            // }
            // $goodsorder = ordergoods::getordergoodsinfo($shoporder->id);
            // foreach ($goodsorder as $key => $value) {
            //     $goods = shopgoods::find(array('id' => $value['goods_id'], 'is_delete' => 0))->getOne();
            //     if (!$goods->id()) {
            //         $this->appErrCode['2006'] = '订单商品不存在或已下架';
            //         $this->responseMsg(2006);
            //     }
            // }

            // $check_max_buy = shoporder::checkOrderLimitBuy($shoporder->id);
            // if ($check_max_buy['code'] != 0) {
            //     if (!empty($check_max_buy['msg'])) {
            //         $this->appErrCode[$check_max_buy['code']] = $check_max_buy['msg'];
            //     }
            //     $this->responseMsg($check_max_buy['code']);
            // }
        }

        $data = ModelCoreorder::payorder($order_info, $user->openid);
        if (!empty($data['code'])) {
            $this->responseMsg(2006,'',$data['msg']);
        }
        //②、统一下单
        //
        $this->responseMsg(0, $data);
    }

    function notify($needSign = true)
    {

        /**
         * 微信支付回调接口
         *s
         * @return string 返回成功或者失败。
         */
        $xml = file_get_contents('php://input', 'r');
        // $xml = '<xml><appid><![CDATA[wx9012b2eab49de625]]></appid>
        // <attach><![CDATA[{"user_id":6,"payfee":0.01}]]></attach>
        // <bank_type><![CDATA[OTHERS]]></bank_type>
        // <cash_fee><![CDATA[1]]></cash_fee>
        // <fee_type><![CDATA[CNY]]></fee_type>
        // <is_subscribe><![CDATA[N]]></is_subscribe>
        // <mch_id><![CDATA[1625426863]]></mch_id>
        // <nonce_str><![CDATA[rb8iout7b71b3uawvyildhx5msv6cay9]]></nonce_str>
        // <openid><![CDATA[ovAQy4wMcrhI1DD5R6rNvK_UPwos]]></openid>
        // <out_trade_no><![CDATA[20220525155949f5a2f2d]]></out_trade_no>
        // <result_code><![CDATA[SUCCESS]]></result_code>
        // <return_code><![CDATA[SUCCESS]]></return_code>
        // <sign><![CDATA[1CEEC5B5DBA29E94699376A4F3755A9D]]></sign>
        // <time_end><![CDATA[20220525155957]]></time_end>
        // <total_fee>1</total_fee>
        // <trade_type><![CDATA[JSAPI]]></trade_type>
        // <transaction_id><![CDATA[4200001531202205256950468942]]></transaction_id>
        // </xml>';
        // Log::write('微信回调'.$xml,'notify');
        //当返回false的时候，表示notify中调用NotifyCallBack回调失败获取签名校验失败，此时直接回复失败
        $handle = new WxPayNotify();
        $handle->ImportHandle($xml);
    }
}
